/*
 * 描述
 * Created by: litangyu
 * 包含类名列表
 * 版本信息，版本号 v1.0
 * 创建日期 16/8/30
 * 版权声明
*/
package com.foton.logistic.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.foton.logistic.R;
import com.foton.logistic.listener.IOnOrderClickListener;
import com.foton.logistic.model.order.OrderEntity;
import com.foton.logistic.model.order.StatusEntity;
import com.foton.logistic.util.OptionUtil;
import com.foton.logistic.util.TimeUtil;
import com.foton.logistic.view.ultimate.UltimateViewAdapter;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 描述
 * Created by: litangyu<br/>
 * Created on: 16/8/30 下午3:55
 */
public class OrderListAdapter extends UltimateViewAdapter {

    private Context mContext;
    private List<OrderEntity> list;

    private IOnOrderClickListener iOnOrderClickListener;

    public OrderListAdapter(Context context, List<OrderEntity> list) {
        this.mContext = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        View view;
        MyViewHolder viewHolder;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout
                .LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ft_adapter_order_item,
                null);
        view.setLayoutParams(layoutParams);
        viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public int getAdapterItemCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        if (position < getItemCount() && (customHeaderView != null ? position <= list.size() :
                position < list.size()) && (customHeaderView != null ? position > 0 : true)) {

            final MyViewHolder viewHolder = (MyViewHolder) holder;
            OrderEntity orderEntity = list.get(holder.getAdapterPosition());
            String id = String.format(mContext.getString(R.string.order_id), orderEntity.orderNum);
            viewHolder.orderId.setText(id);
            try {
                viewHolder.startAddress.setText(orderEntity.startPlace.address);
                viewHolder.deliveryAddress.setText(orderEntity.endPlace.address);
            } catch (Exception e) {
                e.printStackTrace();
            }
            viewHolder.deliveryTime.setText(TimeUtil.formatGLWZTime(orderEntity.estimateBeginDate));
            viewHolder.limitTiem.setText(TimeUtil.formatGLWZTime(orderEntity.arriveDate));
            viewHolder.waitCnt.setText(""+orderEntity.waitCnt+mContext.getString(R.string.upload_track));

            StatusEntity statusEntity = OptionUtil.getOrderStatus(mContext, orderEntity);
            viewHolder.orderStatus.setText(statusEntity.name);
            orderEntity.realStatus = statusEntity.status;
            int status = statusEntity.status;
            if (statusEntity.status == OptionUtil.ORDER_STATUS_1 || statusEntity.status ==
                    OptionUtil.ORDER_STATUS_2 || statusEntity.status == OptionUtil.ORDER_STATUS_20
                    || statusEntity.status == OptionUtil.ORDER_STATUS_21 || statusEntity.status
                    == OptionUtil.ORDER_STATUS_3 || statusEntity.status == OptionUtil
                    .ORDER_STATUS_30
                    || statusEntity.status == OptionUtil.ORDER_STATUS_4|| statusEntity.status == OptionUtil.ORDER_STATUS_41) {
                viewHolder.orderStatus.setTextColor(mContext.getResources().getColor(R.color.button_red));
            } else {
                viewHolder.orderStatus.setTextColor(mContext.getResources().getColor(R.color.button_green));
            }
            viewHolder.btnLayout.setVisibility(View.VISIBLE);
            viewHolder.btnException.setVisibility(View.GONE);
            viewHolder.btnOpt.setVisibility(View.GONE);
            viewHolder.btnrej.setVisibility(View.GONE);
            setButtonRecordVisibility(viewHolder.btnRecord,viewHolder.waitCnt,orderEntity.status,false);
            switch (status) {
                case OptionUtil.ORDER_STATUS_1:
                    viewHolder.btnrej.setVisibility(View.VISIBLE);
                    viewHolder.btnOpt.setVisibility(View.VISIBLE);
                    viewHolder.btnException.setVisibility(View.VISIBLE);
                    viewHolder.btnOpt.setText(mContext.getString(R.string.order_status_55));
                    setButtonRecordVisibility(viewHolder.btnRecord,viewHolder.waitCnt,orderEntity.status,true);
                    viewHolder.btnRecord.setVisibility(View.VISIBLE);
                    break;
                case OptionUtil.ORDER_STATUS_2:
                    viewHolder.btnOpt.setVisibility(View.VISIBLE);
                    viewHolder.btnOpt.setText(mContext.getString(R.string.order_status_8));
                    viewHolder.btnException.setVisibility(View.VISIBLE);
                    break;
                case OptionUtil.ORDER_STATUS_20:
                    viewHolder.btnException.setVisibility(View.VISIBLE);
                    viewHolder.btnOpt.setVisibility(View.VISIBLE);
                    viewHolder.btnOpt.setText(mContext.getString(R.string.order_status_start));
                    setButtonRecordVisibility(viewHolder.btnRecord,viewHolder.waitCnt,orderEntity.status,true);
                    break;
                case OptionUtil.ORDER_STATUS_21:
                    viewHolder.btnException.setVisibility(View.VISIBLE);
                    setButtonRecordVisibility(viewHolder.btnRecord,viewHolder.waitCnt,orderEntity.status,true);
                    break;
                case OptionUtil.ORDER_STATUS_3:
                    viewHolder.btnException.setVisibility(View.VISIBLE);
                    viewHolder.btnOpt.setVisibility(View.VISIBLE);
                    viewHolder.btnOpt.setText(mContext.getString(R.string.order_status_9));
                    setButtonRecordVisibility(viewHolder.btnRecord,viewHolder.waitCnt,orderEntity.status,true);
                    break;
                case OptionUtil.ORDER_STATUS_30:
                    if (orderEntity.signEnd!=null&&orderEntity.signEnd.operateTime!=null){
                        long signEndTime = Long.parseLong(TimeUtil.tranFormatTime(TimeUtil.formatGLWZTime(orderEntity.signEnd.operateTime),"yyyy-MM-dd HH:mm:ss"));
                        if ((System.currentTimeMillis() - signEndTime) <604800000){
                            viewHolder.btnException.setVisibility(View.VISIBLE);
                            viewHolder.btnOpt.setVisibility(View.VISIBLE);
                            viewHolder.btnOpt.setText(mContext.getString(R.string.order_status_complete));
                            setButtonRecordVisibility(viewHolder.btnRecord,viewHolder.waitCnt,orderEntity.status,true);
                        }
                    }
                    break;
                case OptionUtil.ORDER_STATUS_4:
                    viewHolder.btnException.setVisibility(View.VISIBLE);
                    viewHolder.btnOpt.setVisibility(View.VISIBLE);
                    viewHolder.btnOpt.setText(mContext.getString(R.string.order_status_9));
                    break;
                case OptionUtil.ORDER_STATUS_40:
                    viewHolder.btnException.setVisibility(View.VISIBLE);
                    break;
                case OptionUtil.ORDER_STATUS_41:
                    if (orderEntity.signEnd!=null&&orderEntity.signEnd.operateTime!=null){
                        long signEndTime = Long.parseLong(TimeUtil.tranFormatTime(TimeUtil.formatGLWZTime(orderEntity.signEnd.operateTime),"yyyy-MM-dd HH:mm:ss"));
                        if ((System.currentTimeMillis() - signEndTime) <604800000){
                            viewHolder.btnException.setVisibility(View.VISIBLE);
                            viewHolder.btnOpt.setVisibility(View.VISIBLE);
                            viewHolder.btnOpt.setText(mContext.getString(R.string.order_status_complete));
                        }
                    }

                    break;
                default:
                    viewHolder.btnLayout.setVisibility(View.GONE);
                    break;
            }
            //暂时屏蔽
            //viewHolder.btnrej.setVisibility(View.GONE);

            viewHolder.itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iOnOrderClickListener != null) {
                        iOnOrderClickListener.onItemClick(position);
                    }
                }
            });
            viewHolder.btnrej.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iOnOrderClickListener != null) {
                        iOnOrderClickListener.onRejClick(position);
                    }
                }
            });
            viewHolder.btnException.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iOnOrderClickListener != null) {
                        iOnOrderClickListener.onExceptionClick(position);
                    }
                }
            });
            viewHolder.btnOpt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iOnOrderClickListener != null) {
                        iOnOrderClickListener.onOptClick(position);
                    }
                }
            });
            viewHolder.btnRecord.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iOnOrderClickListener != null) {
                        iOnOrderClickListener.onRecordClick(position);
                    }
                }
            });
        }
    }

    public void updateTrackWaitCnt(int waitCnt){
        for (OrderEntity orderEntity : list){
            orderEntity.waitCnt = waitCnt;
        }
        notifyDataSetChanged();
    }

    public void setiOnOrderClickListener(IOnOrderClickListener iOnOrderClickListener) {
        this.iOnOrderClickListener = iOnOrderClickListener;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.ft_adapter_order_item_layout)
        public LinearLayout itemLayout;
        @InjectView(R.id.ft_ui_adapter_order_start_address)
        public TextView startAddress;
        @InjectView(R.id.ft_ui_adapter_order_delivery_address)
        public TextView deliveryAddress;
        @InjectView(R.id.ft_ui_adapter_order_delivery_time)
        public TextView deliveryTime;
        @InjectView(R.id.ft_ui_adapter_order_limit_time)
        public TextView limitTiem;
        @InjectView(R.id.ft_ui_adapter_order_btn_layout)
        public LinearLayout btnLayout;
        @InjectView(R.id.ft_ui_adapter_order_btn_exception)
        public TextView btnException;
        @InjectView(R.id.ft_ui_adapter_order_btn_opt)
        public TextView btnOpt;
        @InjectView(R.id.ft_ui_adapter_order_record)
        public TextView btnRecord;
        @InjectView(R.id.ft_ui_adapter_order_wait_cnt)
        public TextView waitCnt;
        @InjectView(R.id.ft_ui_adapter_order_btn_rej)
        public TextView btnrej;
        @InjectView(R.id.ft_ui_adapter_order_id)
        public TextView orderId;
        @InjectView(R.id.ft_ui_adapter_order_status)
        public TextView orderStatus;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

    private void setButtonRecordVisibility(TextView btnRecord,TextView waitCnt,int status,boolean isVisible){
        if (isVisible){
            if (status==2||status==3){
                btnRecord.setVisibility(View.VISIBLE);
                waitCnt.setVisibility(View.VISIBLE);
            }
        }else {
            btnRecord.setVisibility(View.GONE);
            waitCnt.setVisibility(View.GONE);
        }
    }
}
